{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组\n",
    "### 利用Python创建一个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n",
      "[0 1 2 3 4]\n",
      "[0 1 2 3 4]\n",
      "[0.         1.57079633 3.14159265 4.71238898 6.28318531]\n",
      "3\n",
      "多维数组\n",
      "[[11 12 13]\n",
      " [14 15 16]]\n",
      "13\n"
     ]
    }
   ],
   "source": [
    "# 最基本的方法是将序列传递给NumPy的array()函数; 你可以传递任何序列（类数组），而不仅仅是常见的列表（list）数据类型。\n",
    "\n",
    "import numpy as np\n",
    "a = np.array([0,1,2,3,4])\n",
    "b = np.array([0,1,2,3,4])\n",
    "\n",
    "c = np.arange(5)\n",
    "d = np.linspace(0, 2*np.pi, 5)\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "print(d)\n",
    "print(a[3])\n",
    "\n",
    "# 创建多维数组\n",
    "e = np.array([[11,12,13],\n",
    "              [14,15,16]])\n",
    "print('多维数组')\n",
    "print(e)\n",
    "print(e[0,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多维数组的切片\n",
    "### 通过对每个以逗号分隔的维度执行单独的切片，你可以对多维数组进行切片。因此，对于2D数组，我们的第一片定义了行的切片，第二片定义了列的切片。\n",
    "### 注意，只需输入数字就可以指定行或列。上面的第一个示例从数组中选择第0列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12 13]\n",
      "[14 17]\n",
      "[[11 13]\n",
      " [17 19]]\n",
      "[12 15 18]\n"
     ]
    }
   ],
   "source": [
    "# 对于2D数组，我们的第一片定义了行的切片，第二片定义了列的切片\n",
    "# 切片操作 下标1(不写默认是0)：下标2（不包括，不写默认到结尾） ： 步长\n",
    "a = np.array([[11,12,13],\n",
    "              [14,15,16],\n",
    "              [17,18,19]])\n",
    "\n",
    "print(a[0, 1:4])\n",
    "print(a[1:3, 0])\n",
    "print(a[::2, ::2])\n",
    "print(a[:,1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组的属性\n",
    "### 数组.dtypt--》元素的类型    数组.size--》数组一共有几个元素   数组.shape--》数组是几行几列\n",
    "### itemsize属性是每个项占用的字节数。这个数组的数据类型是int 64，一个int 64中有64位，一个字节中有8位，除以64除以8，你就可以得到它占用了多少字节，在本例中是8\n",
    "### ndim 属性是数组的维数。这个有2个。例如，向量只有1。\n",
    "### nbytes 属性是数组中的所有数据消耗掉的字节数。你应该注意到，这并不计算数组的开销，因此数组占用的实际空间将稍微大一点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "int32\n",
      "25\n",
      "(5, 5)\n",
      "4\n",
      "2\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28 ,29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "print(type(a))  # <class 'numpy.ndarray'>\n",
    "print(a.dtype)  # int32\n",
    "print(a.size)\n",
    "print(a.shape)  # (5, 5)\n",
    "print(a.itemsize)  # 32位/8位/字节 = 4字节\n",
    "print(a.ndim)\n",
    "print(a.nbytes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cumsum()，对数组进行求和操作，例如[0,1,2]  计算结果[0,1,3]  0=0,1=0+1,3=0+1+2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  3  6 10 15 21 28 36 45]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "print(a.cumsum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引进阶\n",
    "### 花式索引：是获取数组中我们想要的特定元素的有效方法\n",
    "### 在数组中传入一个包含我们想要的数组元素下标的列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0 10 20 30 40 50 60 70 80 90]\n",
      "[10 50 90]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(0, 100, 10)\n",
    "indices = [1,5,-1]\n",
    "b = a[indices]\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔屏蔽：是一个有用的功能，它允许我们根据我们指定的条件检索数组中的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1e477b9b128>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Boolean masking\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "a = np.linspace(0, 2 * np.pi, 50)\n",
    "b = np.sin(a)\n",
    "plt.plot(a,b)\n",
    "mask = b >= 0\n",
    "plt.plot(a[mask], b[mask], 'bo')\n",
    "mask = (b >= 0) & (a <= np.pi / 2)\n",
    "plt.plot(a[mask], b[mask], 'go')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 缺省索引\n",
    "### 不完全索引是从多维数组的第一个维度获取索引或切片的一种方便方法。例如，如果数组a=[1，2，3，4，5]，[6，7，8，9，10]，那么[3]将在数组的第一个维度中给出索引为3的元素，这里是值4。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0 10 20 30 40]\n",
      "[50 60 70 80 90]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(0,100,10)\n",
    "b = a[:5]\n",
    "c = a[a >= 50]\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Where 函数\n",
    "### where() 函数是另外一个根据条件返回数组中的值的有效方法。只需要把条件传递给它，它就会返回一个使得条件为真的元素下标的列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0 10 20 30 40 50 60 70 80 90]\n",
      "(array([0, 1, 2, 3, 4], dtype=int64),)\n",
      "[5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(0, 100, 10)\n",
    "print(a)\n",
    "b = np.where(a < 50)  # 返回的是一个使得条件成立的元素下标的列表\n",
    "c = np.where(a >= 50)[0]\n",
    "print(b)\n",
    "print(c)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
